import win32api
import win32con
import win32gui
import os
import time
import argparse

def setWallpaper(imgPath):
        #打开注册表
        reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)

        #2：拉伸  0：居中  6：适应  10：填充
        win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "2")

        #win32api.RegSetValueEx(reg_key,"Wallpaper")

        #SPIF_SENDWININICHANGE:立即生效
        win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, imgPath, win32con.SPIF_SENDWININICHANGE)

def getImgPath(filePath):
    imgPathList = []
    for filename in os.listdir(filePath):
        imgPathList.append(os.path.join(filePath,filename))
    return imgPathList

def main():
    # 默认壁纸文件夹路径
    default_path = r'C:\Users\dingj\Desktop\壁纸';
    # 默认延时更换壁纸的时间
    default_delay_time = 3
    # 解析参数
    parser = argparse.ArgumentParser(description="argparse")
    parser.add_argument('-p','--path', default=default_path)
    parser.add_argument('-t','--time', default=default_delay_time)
    args = parser.parse_args()
    path = args.path
    delay_time = float(args.time)

    print(f'path: {path}, time: {delay_time}')

    imgPathList = getImgPath(path)
    print(imgPathList)

    while True:
        for imgPath in imgPathList:
            setWallpaper(imgPath)
            time.sleep(delay_time)


if __name__ == '__main__':
    main()
